package org.micode.common.domain.vo;

import org.micode.common.utils.FileExtUtils;
import org.micode.common.utils.StringExtUtils;
import lombok.Data;
import org.springframework.http.MediaType;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Data
public class FileVO {

    /**
     * 文件名
     */
    private String filename;

    /**
     * URL地址
     */
    private String url;

    private String extName;

    /**
     * 源文件名，在下载时可以使用这个
     */
    private String sourceName;

    private final Map<String, String> EXT_MAP = new HashMap<String, String>() {{
        put(".mp3", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".MP3", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".wav", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".WAV", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".png", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".PNG", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".jpg", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".jpeg", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".mp4", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".MP4", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".svg", "image/svg+xml; charset=UTF-8");
        put(".SVG", "image/svg+xml; charset=UTF-8");
        put(".JSON", MediaType.APPLICATION_JSON_UTF8_VALUE);
        put(".json", MediaType.APPLICATION_JSON_UTF8_VALUE);
        put(".zip", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".ZIP", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        put(".sql", MediaType.TEXT_PLAIN_VALUE);
        put(".SQL", MediaType.TEXT_PLAIN_VALUE);
    }};

    public String readMediaType() {
        String ret = EXT_MAP.get(extName);
        if (null == ret) {
            ret = MediaType.APPLICATION_OCTET_STREAM_VALUE;
        }
        return ret;
    }

    public FileVO() {
    }

    public FileVO(String filename, String url, String extName) {
        this.url = url;
        this.filename = filename;
        this.extName = extName;

        this.sourceName = filename;
    }

    public static FileVO buildTmpFile(String extName) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String url = String.format("/tmp/%s.%s", uuid, extName);
        if (System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            url = String.format("d:/temp/%s.%s", uuid, extName);
        }
        String filename = String.format("%s.%s", uuid, extName);
        return new FileVO(filename, url, extName);
    }

    public static FileVO buildFileVO(String fileUrl) {
        return buildFileVO(fileUrl, false);
    }

    public static FileVO buildFileVO(String fileUrl, boolean includedExt) {
        if (StringExtUtils.isAnyEmpty(fileUrl)) {
            return null;
        }

        String filePath = fileUrl.trim();

        String[] dirs = filePath.split("/");
        int cnt = dirs.length;
        String filename = dirs[cnt - 1];
        String ext = FileExtUtils.readExtName(filename);
        if (StringExtUtils.isAnyEmpty(ext)) {
            ext = ".mp3";
        } else {
            ext = "." + ext;
        }

        if (includedExt) {
            return new FileVO(filename, filePath, ext);
        }
        return new FileVO(filename + ext, filePath + ext, ext);
    }

    public static void main(String[] args) {
        System.out.println(
                buildFileVO("/home/micode/MICSYS/AGTStream/record/360000000000000000/68781162411065344/7.png", true));
    }

}
